{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、组合数据类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.1 列表：list （有序可变）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a[0]\n",
    "a[0] = 100\n",
    "print(a[0])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T09:50:44.286795800Z",
     "start_time": "2024-11-16T09:50:44.261698500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2 元组：tuple（有序不可变）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[6], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m b \u001B[38;5;241m=\u001B[39m (\u001B[38;5;241m1\u001B[39m,\u001B[38;5;241m2\u001B[39m,\u001B[38;5;241m3\u001B[39m,\u001B[38;5;241m4\u001B[39m,\u001B[38;5;241m5\u001B[39m)\n\u001B[1;32m----> 2\u001B[0m \u001B[43mb\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m \u001B[38;5;241m=\u001B[39m\u001B[38;5;241m100\u001B[39m\n",
      "\u001B[1;31mTypeError\u001B[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "b = (1,2,3,4,5)\n",
    "#b[0] =100 报错\n",
    "b[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T09:50:57.287780Z",
     "start_time": "2024-11-16T09:50:56.864718100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.3 字典：dict（无序可变，不能通过位序访问，只能通过key访问置）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "c = {'a':1, 'b':2, 'c':3}\n",
    "c['a']"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.4 集合：set（无序不可变，并且元素不重复）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "{1, 2, 3, 4, 5}"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {1,1,2,3,4,5}\n",
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T09:52:59.563489300Z",
     "start_time": "2024-11-16T09:52:59.548484800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、变量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 打包赋值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x,y,z = 1,2,3\n",
    "print(x,y,z)\n",
    "x,y=y,x\n",
    "print(x,y,z)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 三、控制流程"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.1 for\n",
    "主要形式：\n",
    "for 变量 in 可迭代对象:\n",
    "    语句\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "#实现1-5求和\n",
    "res = 0\n",
    "for i in [1,2,3,4,5]:\n",
    "    res += i\n",
    "print(res)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T09:59:03.708480200Z",
     "start_time": "2024-11-16T09:59:03.686386200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 while\n",
    "主要形式：\n",
    "while 条件:\n",
    "    语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#实现1-5求和\n",
    "res = 0\n",
    "i = 1\n",
    "while i <= 5:\n",
    "    res += i\n",
    "    i += 1\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 四、输入输出"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.1 输入\n",
    "input 输入的是字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": "str"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = input(\"请输入:\")\n",
    "print(a)\n",
    "type(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T10:02:09.561567500Z",
     "start_time": "2024-11-16T10:02:07.672984700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.5\n"
     ]
    },
    {
     "data": {
      "text/plain": "float"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用eval将输入的字符串转换成数字\n",
    "b = eval(input(\"请输入:\"))\n",
    "print(b)\n",
    "type(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T10:03:03.265749300Z",
     "start_time": "2024-11-16T10:03:01.591185800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2 输出"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hello\n",
      "hello\n",
      "world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "#打印字符串\n",
    "print(\"hello\")\n",
    "#打印变量\n",
    "a = \"hello\"\n",
    "print(a)\n",
    "#注意print输出自带换行符\n",
    "print(\"hello\")\n",
    "print(\"world\")\n",
    "#利用换行控制end=\n",
    "print(\"hello\",end=\" \")\n",
    "print(\"world\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T10:06:08.355071600Z",
     "start_time": "2024-11-16T10:06:08.337074700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.3 复杂输出，多个变量一起输出"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PI= 3.14 E= 2.71\n",
      "PI=3.14,E=2.71\n",
      "PI=2.71,E=3.14\n",
      "PI=3.14,E=3.14\n"
     ]
    }
   ],
   "source": [
    "PI=3.14\n",
    "E=2.71\n",
    "print(\"PI=\",PI,\"E=\",E)\n",
    "#格式化输出format,基本格式：“字符串{0}字符串{1}\".format(v0,v1)\n",
    "print(\"PI={0},E={1}\".format(PI,E))\n",
    "print(\"PI={1},E={0}\".format(PI,E))\n",
    "print(\"PI={0},E={0}\".format(PI,E))\n",
    "#format填充输出\n",
    "#___3.1415926___进行填充\n",
    "print(\"{0:_^10}\".format(PI))#0表示位置在0处的变量，^表示居中，10表示宽度为10，_表示用“_”填充字符\n",
    "print(\"{0:*>10}\".format(PI))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T10:12:35.778098500Z",
     "start_time": "2024-11-16T10:12:35.767100Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
